package com.anbao.newanbao;

import com.anbao.action.ActivityManagerAction;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class InputPwdActivity extends Activity implements
		android.view.View.OnClickListener {

	private Button btn_0;
	private Button btn_1;
	private Button btn_2;
	private Button btn_3;
	private Button btn_4;
	private Button btn_5;
	private Button btn_6;
	private Button btn_7;
	private Button btn_8;
	private Button btn_9;
	private Button btn_clean;

	private View view1;
	private View view2;
	private View view3;
	private View view4;
	
	private String pwdStr="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_input_pwd);
		//将当前界面加入到系统管理中国
		ActivityManagerAction.getScreenManager().pushActivity(this);
		iniView();
	}

	private void iniView() {

		btn_0 = (Button) findViewById(R.id.btn_0);
		btn_0.setOnClickListener(this);
		btn_1 = (Button) findViewById(R.id.btn_1);
		btn_1.setOnClickListener(this);
		btn_2 = (Button) findViewById(R.id.btn_2);
		btn_2.setOnClickListener(this);
		btn_3 = (Button) findViewById(R.id.btn_3);
		btn_3.setOnClickListener(this);
		btn_4 = (Button) findViewById(R.id.btn_4);
		btn_4.setOnClickListener(this);
		btn_5 = (Button) findViewById(R.id.btn_5);
		btn_5.setOnClickListener(this);
		btn_6 = (Button) findViewById(R.id.btn_6);
		btn_6.setOnClickListener(this);
		btn_7 = (Button) findViewById(R.id.btn_7);
		btn_7.setOnClickListener(this);
		btn_8 = (Button) findViewById(R.id.btn_8);
		btn_8.setOnClickListener(this);
		btn_9 = (Button) findViewById(R.id.btn_9);
		btn_9.setOnClickListener(this);
		btn_clean = (Button) findViewById(R.id.cleanPwdBtn);
		btn_clean.setOnClickListener(this);

		view1 = findViewById(R.id.pwd1_view);
		view2 = findViewById(R.id.pwd2_view);
		view3 = findViewById(R.id.pwd3_view);
		view4 = findViewById(R.id.pwd4_view);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btn_0:

			inputPwd(0);
			
			break;
		case R.id.btn_1:
			inputPwd(1);
			break;
		case R.id.btn_2:
			inputPwd(2);
			break;
		case R.id.btn_3:
			inputPwd(3);
			break;
		case R.id.btn_4:
			inputPwd(4);
			break;
		case R.id.btn_5:
			inputPwd(5);
			break;
		case R.id.btn_6:
			inputPwd(6);
			break;
		case R.id.btn_7:
			inputPwd(7);
			break;
		case R.id.btn_8:
			inputPwd(8);
			break;
		case R.id.btn_9:
			inputPwd(9);
			break;
		case R.id.cleanPwdBtn:
			cleanPwd();
			break;

		}
	}

	private void cleanPwd() {
		
		
		pwdStr = "";
		
		view1.setBackgroundColor(Color.WHITE);
		view2.setBackgroundColor(Color.WHITE);
		view3.setBackgroundColor(Color.WHITE);
		view4.setBackgroundColor(Color.WHITE);
		
	}

	private void inputPwd(int num) {
		
		
		if(pwdStr.length()<4){
			
			pwdStr+=num+"";
			
			if(pwdStr.length()==4){
				
				view4.setBackgroundColor(R.color.grey);
				
				if(pwdStr.endsWith("1234")){
					setResult(RESULT_OK, new Intent().setAction("true"));
					finish();
				}
			}else if(pwdStr.length()==3){
				view3.setBackgroundColor(R.color.grey);
				
			}else if(pwdStr.length()==2){
				
				view2.setBackgroundColor(R.color.grey);
			}else if(pwdStr.length()==1){
				
				view1.setBackgroundColor(R.color.grey);
			
			}
		}
		
	
		
	}

}
